package com.sentiment.database.bean;

import java.util.Date;

import org.bson.BsonDocument;
import org.bson.BsonDocumentWrapper;
import org.bson.Document;
import org.bson.codecs.configuration.CodecRegistry;
import org.bson.types.ObjectId;

/**
 * 存储爬取任务的类，是WebCollector的核心类，记录了一个url的爬取信息，同样也 可以作为一个爬取任务
 * 
 * @author 王骏科
 *
 */
@SuppressWarnings("serial")
public class Datum extends Document {

	/**
	 * 爬取状态常量-注入
	 */
	public static final int STATUS_DB_INJECTED = 0x01;
	/**
	 * 爬取状态常量-未爬取
	 */
	public static final int STATUS_DB_UNFETCHED = 0x04;
	/**
	 * 爬取状态常量-已爬取
	 */
	public static final int STATUS_DB_FETCHED = 0x05;

	ObjectId _id;
	// 爬取时间
	Date date;
	// 爬取状态
	int status;
	// 爬取的URL
	String url;
	// 重试次数
	int retry;
	// 标记该次爬取的id字段
	// 多个爬取源记录对应同一个值，都为一次爬取
	ObjectId mark;
	// 该次爬取的深度
	int depth;
	// 重定向的url
	String realUrl;

	public void setUpdate(String method, Datum n) {
		append(method, n);
	}
	public String getRealUrl() {
		return getString("realUrl");
	}

	public void setRealUrl(String realUrl) {
		append("realUrl", realUrl);
	}

	public ObjectId getMark() {
		return getObjectId("mark");
	}

	public void setMark(ObjectId mark) {
		append("mark", mark);
	}

	public int getDepth() {
		return getInteger("depth");
	}

	public void setDepth(int depth) {
		append("depth", depth);
	}

	public Datum() {
		// TODO Auto-generated constructor stub
	}

	public Datum(ObjectId _id) {
		append("_id", _id);
	}

	public Datum(String url, int status, int retry) {
		super();
		append("url", url);
		append("status", status);
		append("retry", retry);
	}

	public int getRetry() {
		return getInteger("retry");
	}

	public void setRetry(int retry) {
		append("retry", retry);
	}

	public ObjectId get_id() {
		return getObjectId("_id");
	}

	public void set_id(ObjectId _id) {
		append("_id", _id);
	}

	public Date getDate() {
		return getDate("date");
	}

	public void setDate(Date date) {
		append("date", date);
	}

	public int getStatus() {
		return getInteger("status");
	}

	public void setStatus(int status) {
		append("status", status);
	}

	public String getUrl() {
		return getString("url");
	}

	public void setUrl(String url) {
		append("url", url);

	}

	@Override
	public <TDocument> BsonDocument toBsonDocument(Class<TDocument> documentClass, CodecRegistry codecRegistry) {
		// TODO Auto-generated method stub
		return new BsonDocumentWrapper<Datum>(this, codecRegistry.get(Datum.class));
	}
}
